(function($) {
	"use strict";
	var calendarSwitch1 = (function() {
		function calendarSwitch1(element, options) {
			this.settings = $.extend(true, $.fn.calendarSwitch1.defaults, options || {});
			this.element = element;
			this.init();
		}
		calendarSwitch1.prototype = { /*说明：初始化插件*/
			/*实现：初始化dom结构，布局，分页及绑定事件*/
			init: function() {
				var me = this;
				me.selectors = me.settings.selectors;
				me.sections = me.selectors.sections;
				me.index = me.settings.index;
				me.comfire = me.settings.comfireBtn;

				var html = "<div class='headerWrapper'><div class='prev'><div class='swiper-button-prev swiper-button-black'></div></div><div class='thisTime'></div><div class='next'><div class='swiper-button-next swiper-button-black'></div></div></div><table class='dateZone'><tr><td >日</td><td>一</td><td>二</td><td>三</td><td>四</td><td>五</td><td class='colo'>六</td></tr></table>" + "<div class='swiper-container'><div class='swiper-wrapper tbody'></div></div>"
				$(me.sections).append(html);
				$(me.sections).find('.headerWrapper').css({
					"height": "1rem",
					"line-height": "1rem",
					"width": "100%",
					"background": "#fff",
					"padding": "0px .5rem"
				});
				var currentDate = new Date();
				currentDate.setDate(1);
				currentDate.setMonth(currentDate.getMonth());
				var currentYear = currentDate.getFullYear();
				var currentMonth = currentDate.getMonth();
				var setcurrentDate = new Date(currentYear, currentMonth, 1);
				var firstDay = setcurrentDate.getDay();
				var yf = currentMonth + 1;
				if(yf < 10) {
					$('.thisTime').html(currentYear + '年' + '<span class=time>0' + yf + '</span>月');
				} else {
					$('.thisTime').html(currentYear + '年<span class=time>' + yf + '</span>月');
				}
				
				for(var q = 0; q < me.index; q++) {
					var select = q;
					$(me.sections).find(".tbody").append("<table class='swiper-slide dateTable'></table>")
					var currentDate = new Date();
					currentDate.setDate(1);
					currentDate.setMonth(currentDate.getMonth() + select);
					var currentYear = currentDate.getFullYear();
					var currentMonth = currentDate.getMonth();
					var setcurrentDate = new Date(currentYear, currentMonth, 1);
					var firstDay = setcurrentDate.getDay();
					var yf = currentMonth + 1;
					var DaysInMonth = [];
					if(me._isLeapYear(currentYear)) {
						DaysInMonth = new Array(31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
					} else {
						DaysInMonth = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
					}
					var Ntd = firstDay + DaysInMonth[currentMonth];
					var Ntr = Math.ceil(Ntd / 7);
					for(var i = 0; i < Ntr; i++) {
						$(me.sections).find('.dateTable').eq(select).append('<tr></tr>');
					};
					var createTd = $(me.sections).find('.dateTable').eq(select).find('tr');
					createTd.each(function(index, element) {
						for(var j = 0; j < 7; j++) {
							$(this).append('<td></td>')
						}
					});
					var arryTd = $(me.sections).find('.dateTable').eq(select).find('td');
					var meny = "";
					for(var m = 0; m < DaysInMonth[currentMonth]; m++) {
						var htmlStr = "<span class='day'>" + (m + 1) + "</span>" + "<p style='color: gainsboro;' class='meny'>" + meny + "</p>"
						arryTd.eq(firstDay++).html(htmlStr);
					}
				}
				me._initselected();
				me.element.on('click', function(event) {
					event.preventDefault();
				});

			},
			_isLeapYear: function(year) {
				return(year % 4 == 0) && (year % 100 != 0 || year % 400 == 0);
			},
			_initselected: function() {
				var me = this;
				me.comeColor = me.settings.comeColor;
				me.outColor = me.settings.outColor;
				me.daysnumber = me.settings.daysnumber;
				var strDays = new Date().getDate();
				var arry = [];
				var arry1 = [];
				var tds = $(me.sections).find('.dateTable').eq(0).find('td');
				tds.each(function(index, element) {
					if($(this).text() == strDays) {
						var r = index;
						//						$(this).prepend('<p class="rz">起</p>');
						if($(this).next().text() != "") {
							//							$(this).next().prepend('</br><p class="rz">止</p>');

						} else {
							$(".dateTable").eq(1).find("td").each(function(index, el) {
								if($(this).text() != "") {
									//									$(this).prepend('</br><p class="rz">止</p>');
									return false;
								}
							});
						}
						me._checkColor(me.comeColor, me.outColor);

					}
				})

				$(me.sections).find('.tbody').find('td').each(function(index, element) {
					if($(this).text() != '') {
						arry.push(element);
					}
				});

				for(var i = 0; i < strDays - 1; i++) {
					$(arry[i]).css('color', '#ccc');
					$(arry[i]).addClass("cccDay");
				}
				for(var i = 0; i < $(".cccDay").length - 1; i++) {
					$(".cccDay").eq(i).find(".meny").remove();

				}
				if(me.daysnumber) {
					//可以在这里添加90天的条件
					for(var i = strDays - 1; i < strDays + parseInt(me.daysnumber); i++) {
						arry1.push(arry[i])
					}
					for(var i = strDays + parseInt(me.daysnumber); i < $(arry).length; i++) {
						$(arry[i]).css('color', '#ccc')
					}
				} else {
					for(var i = strDays - 1; i < $(arry).length; i++) {
						arry1.push(arry[i])
					}
				}

				me._selectDate(arry1)
			},
			_checkColor: function(comeColor, outColor) {
				var me = this;
				var rz = $(me.sections).find('.rz');
				for(var i = 0; i < rz.length; i++) {
					if(rz.eq(i).text() == "起") {
						rz.eq(i).closest('td').css({
							'background': comeColor,
							'color': '#fff'
						});
					} else {
						rz.eq(i).closest('td').css({
							'background': outColor,
							'color': '#fff'
						});
					}
				}
			},
			_callback: function() {
				var me = this;
				if(me.settings.callback && $.type(me.settings.callback) === "function") {
					me.settings.callback();
				}
			},
			_selectDate: function(arry1) {
				var me = this;
				me.comeColor = me.settings.comeColor;
				me.outColor = me.settings.outColor;
				me.comeoutColor = me.settings.comeoutColor;
				me.sections = me.selectors.sections;
				var flag = 0;
				var first;
				var sum;
				var second;

				$(arry1).on('click', function(index) {
					var getmeny = $(this).find(".mymeny").text();
					index.stopPropagation();
					index.preventDefault();
					if($('.calendar').css('display') != "none") {
						if(getmeny == "已租") {
							return;
						}
						if(flag == 0) {
							//第一次点击
							$(me.sections).find('.hover').remove();
							$(me.sections).find('.tbody').find('p').remove('.rz');
							$(me.sections).find('.tbody').find('br').remove();
							$(arry1).css({
								'background': '#fff',
								'color': '#333333'
							});
							$(this).prepend('<p class="rz">起</p>');
							var tds = $(".tbody td");
							$(".tbody").find("span").show();
							//							$(this).children("span").hide();
							for(var k = 0; k < tds.length; k++) {
								if(tds.eq(k).children("p").eq(0).hasClass("rz")) {
									tds.eq(k).children("span").hide();
								}
							}
							first = $(arry1).index($(this));
							me._checkColor(me.comeColor, me.outColor)
							flag = 1;
							$('.hover').css({
								'position': 'absolute',
								'left': '-17px',
								'top': '0px'
							})
							$('.ruzhu_hover').css({
								'width': '100%',
								'height': '41px',
								'left': '0px',
								'top': '-45px',
								'background': '#434949',
								'color': '#fff',
								'z-index': '2'
							})
						} else if(flag == 1) { //第二次点击
							$(me.sections).find('.rz').each(function(index, element) {
								if($(this).text() == '起') {
									$(this).parent('td').find('.ruzhu_hover').remove();
									$(this).parent('td').css('position', 'relative');
								}
							});
							flag = 0;
							second = $(arry1).index($(this))
							for(var i = first; i < second; i++) {
								if($(".swiper-slide-active td").eq(i).find(".mymeny").text() == "已租") {
									var str = "<div class='alert'>该时段不可选，该时段包含已出租时间</div>";
									$("body").append(str);
									$(".alert").fadeIn();
									setTimeout(function() {
										$(".alert").fadeOut();
									}, 1000)
									return;
								}
							}
							//如果第一次点击比第二次大，则不显示
							if(first >= second) {
								$(me.sections).find('.hover').remove();
								$(me.sections).find('.tbody').find('p').remove('.rz');
								$(me.sections).find('.tbody').find('br').remove();
								$(arry1).css({
									'background': '#fff',
									'color': '#333333'
								});
								$(this).prepend('<p class="rz">起</p>');
								var tds = $(".tbody td");
								$(".tbody").find("span").show();
								//								$(this).children("span").hide();
								for(var k = 0; k < tds.length; k++) {
									if(tds.eq(k).children("p").eq(0).hasClass("rz")) {
										tds.eq(k).children("span").hide();
									}
								}
								first = $(arry1).index($(this));
								me._checkColor(me.comeColor, me.outColor)
								flag = 1;
								$('.hover').css({
									'position': 'absolute',
									'left': '-17px',
									'top': '0px'
								})
								$('.ruzhu_hover').css({
									'width': '100%',
									'height': '41px',
									'left': '0px',
									'top': '-45px',
									'background': '#434949',
									'color': '#fff',
									'z-index': '2'
								});

								return;
							}

							sum = Math.abs(second - first);
							if(sum == 0) {
								sum = 1;
							}

							if(first < second) {

								$(this).prepend('<p class="rz">止</p>')
								var tds = $(".tbody td");
								$(".tbody").find("span").show();
								//								$(this).children("span").hide();
								for(var k = 0; k < tds.length; k++) {
									if(tds.eq(k).children("p").eq(0).hasClass("rz")) {
										tds.eq(k).children("span").hide();
									}
								}

								first = first + 1;
								for(first; first < second; first++) {
									$(arry1[first]).css({
										'background': me.comeoutColor,
										'color': '#333333'
									});
								}
							} else if(first > second) {
								$(me.sections).find('.rz').text('止');
								$(this).prepend('<p class="rz">起</p>');
								var tds = $(".tbody td");
								$(".tbody").find("span").show();
								//								$(this).children("span").hide();
								for(var k = 0; k < tds.length; k++) {
									if(tds.eq(k).children("p").eq(0).hasClass("rz")) {
										tds.eq(k).children("span").hide();
									}
								}
								second = second + 1;
								for(second; second < first; second++) {
									$(arry1[second]).css({
										'background': me.comeoutColor,
										'color': '#333333'
									});
								}
							}
							//得到总天数
							$(me.sections).find('.rz').each(function(index, element) {
								if($(this).text() == '止') {
									//									alert(sum+"天");
								}
							});
							var myweek = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
							var st = new Date($('#startDate').val());
							var en = new Date($('#endDate').val());
							$('.week').text(myweek[st.getDay()])
							$('.week1').text(myweek[en.getDay()])
							me._checkColor(me.comeColor, me.outColor)
						}
						//第二次点击结束
					}
				})
			}

		}
		return calendarSwitch1;
	})();
	$.fn.calendarSwitch1 = function(options) {
		return this.each(function() {
			var me = $(this),
				instance = me.data("calendarSwitch1");

			if(!instance) {
				me.data("calendarSwitch1", (instance = new calendarSwitch1(me, options)));
			}

			if($.type(options) === "string") return instance[options]();
		});
	};
	$.fn.calendarSwitch1.defaults = {
		selectors: {
			sections: "#calendar"
		},
		index: 4,
		//展示的月份个数
		animateFunction: "toggle",
		//动画效果
		controlDay: false,
		//知否控制在daysnumber天之内，这个数值的设置前提是总显示天数大于90天
		daysnumber: "30",
		//控制天数
		comeColor: "blue",
		//入住颜色
		outColor: "red",
		//离店颜色
		comeoutColor: "#0cf",
		//入住和离店之间的颜色
		callback: "",
		//回调函数
		comfireBtn: '.comfire' //确定按钮的class或者id

	};
})(jQuery);